﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using DBUtility;

namespace OracleDAL.SUBSYSTEM
{
    /// <summary>
    /// 数据访问类SUBSYS。
    /// </summary>
    public class SUBSYS
    {
        /// <summary>
        /// 
        /// </summary>
        public SUBSYS()
        { }
        #region  成员方法
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="SUBSYSTEMID">子系统ID</param>
        /// <param name="MODULEID">模块ID</param>
        /// <returns></returns>
        public Model.SUBSYSTEM.SUBSYS GetModel(string SUBSYSTEMID, string MODULEID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CODE.CODEVALUE AS SUBSYSTEMNAME,SM.SUBSYSTEMID,SM.MODULEID,SM.MODULENAME,SM.MODULERUNSTATE,SM.COLLECTIONTIME,SM.REMARK ");
            strSql.Append(" from CODE,SUBSYS_MODULE SM  ");
            strSql.Append(" where SM.SUBSYSTEMID=:SUBSYSTEMID and MODULEID=:MODULEID and CODE.CODEID=SM.SUBSYSTEMID ");
            OracleParameter[] parameters = {
					new OracleParameter(":SUBSYSTEMID", OracleType.VarChar,50),
					new OracleParameter(":MODULEID", OracleType.VarChar,50)};
            parameters[0].Value = SUBSYSTEMID;
            parameters[1].Value = MODULEID;

            Model.SUBSYSTEM.SUBSYS model = new Model.SUBSYSTEM.SUBSYS();
            List<Model.SUBSYSTEM.SUBSYS_MODULE> moduleList = new List<Model.SUBSYSTEM.SUBSYS_MODULE>();
            Model.SUBSYSTEM.SUBSYS_MODULE module = new Model.SUBSYSTEM.SUBSYS_MODULE();
            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.SUBSYSTEMID = ds.Tables[0].Rows[0]["SUBSYSTEMID"].ToString();
                model.SUBSYSTEMNAME = ds.Tables[0].Rows[0]["SUBSYSTEMNAME"].ToString();
                module.SUBSYSTEMID = ds.Tables[0].Rows[0]["SUBSYSTEMID"].ToString();
                module.SUBSYSTEMNAME = ds.Tables[0].Rows[0]["SUBSYSTEMNAME"].ToString();
                module.MODULEID = ds.Tables[0].Rows[0]["MODULEID"].ToString();
                module.MODULENAME = ds.Tables[0].Rows[0]["MODULENAME"].ToString();
                module.MODULERUNSTATE = ds.Tables[0].Rows[0]["MODULERUNSTATE"].ToString();
                if (ds.Tables[0].Rows[0]["COLLECTIONTIME"].ToString() != "")
                {
                    module.COLLECTIONTIME = DateTime.Parse(ds.Tables[0].Rows[0]["COLLECTIONTIME"].ToString());
                }
                module.REMARK = ds.Tables[0].Rows[0]["REMARK"].ToString();
                moduleList.Add(module);
                model.MODULE = moduleList;
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得模块状态数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CODE.CODEID AS SUBSYSTEMID,CODE.CODEVALUE AS SUBSYSTEMNAME,SM.MODULEID,SM.MODULENAME,SM.MODULERUNSTATE,SM.COLLECTIONTIME,SM.REMARK ");
            strSql.Append(" FROM CODE,SUBSYS_MODULE SM  ");
            strSql.Append(" where CODE.CODEID=SM.SUBSYSTEMID  ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and " + strWhere);
            }
            strSql.Append("  order by CODE.CODEID,SM.MODULEID,SM.MODULENAME ");
            return OracleHelper.Query(strSql.ToString());
        }
        /// <summary>
        /// 获得告警数据列表
        /// </summary>
        public DataSet GetAlarmList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ALARMLEVEL,COUNT(ALARMLEVEL) AS ALARMCOUNT,belongdomain AS SUBSYSTEMNAME,(select codeid from code where codevalue=belongdomain) as SUBSYSTEMID from ALARM_INFO  ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" group by BELONGDOMAIN,ALARMLEVEL  order by BELONGDOMAIN,ALARMLEVEL ");
            return OracleHelper.Query(strSql.ToString());
        }
        /// <summary>
        /// 获取某个子系统告警
        /// </summary>
        /// <param name="SUBSYSTEMID">子系统ID</param>
        /// <returns></returns>
        public List<Model.SUBSYSTEM.SUBSYS_ALARM> GetAlarmModel(string SUBSYSTEMID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ALARMLEVEL,COUNT(ALARMLEVEL) AS ALARMCOUNT, BELONGTRANSYS as SUBSYSTEMID,(select CODEVALUE from CODE where CODEID=BELONGTRANSYS) AS SUBSYSTEMNAME from ALARM_INFO ");
            strSql.Append(" where BELONGTRANSYS=:SUBSYSTEMID ");
            strSql.Append(" group by BELONGTRANSYS,ALARMLEVEL order by BELONGTRANSYS,ALARMLEVEL ");

            OracleParameter[] parameters = {
					new OracleParameter(":SUBSYSTEMID", OracleType.VarChar,50)};
            parameters[0].Value = SUBSYSTEMID;

            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                List<Model.SUBSYSTEM.SUBSYS_ALARM> modelList = new List<Model.SUBSYSTEM.SUBSYS_ALARM>();
                Model.SUBSYSTEM.SUBSYS_ALARM model;
                var length = ds.Tables[0].Rows.Count;
                for (int i = 0; i < length; i++)
                {
                    model = new Model.SUBSYSTEM.SUBSYS_ALARM();
                    model.ALARMLEVEL = ds.Tables[0].Rows[i]["ALARMLEVEL"].ToString();
                    model.ALARMCOUNT = ds.Tables[0].Rows[i]["ALARMCOUNT"].ToString();
                    modelList.Add(model);
                }
                return modelList;
            }
            else
            {
                return null;
            }
        }
        #endregion  成员方法
    }
}

